package com.group56.viewmodel;

import com.group56.CoachApp;
import com.group56.model.dao.CoachDAO;
import com.group56.model.dao.Impl.CoachDAOImpl;
import com.group56.model.entity.Coach;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.control.TextField;

public class CoachInfoVM implements ViewModel{
    private static CoachInfoVM coachInfoVM;
    public static CoachInfoVM getInstance(String coachId){
        if(coachInfoVM == null || !coachId.equals(coachInfoVM.getCoachId())){
            coachInfoVM = new CoachInfoVM(coachId);
        }
        return coachInfoVM;
    }
    private StringProperty coachId;
    private StringProperty name;
    private StringProperty picUrl;
    private StringProperty sex;
    private StringProperty tele;
    private StringProperty detail;
    private StringProperty password;
    private CoachDAO dao = new CoachDAOImpl();

    public CoachInfoVM(String coachId) {
        this.coachId = new SimpleStringProperty(coachId);
        this.name = new SimpleStringProperty();
        this.picUrl = new SimpleStringProperty();
        this.sex = new SimpleStringProperty();
        this.tele = new SimpleStringProperty();
        this.detail = new SimpleStringProperty();
        this.password=new SimpleStringProperty();
        updateFromModel();
    }

    public static CoachInfoVM getCoachInfoVM() {
        return coachInfoVM;
    }

    public static void setCoachInfoVM(CoachInfoVM coachInfoVM) {
        CoachInfoVM.coachInfoVM = coachInfoVM;
    }

    public String getCoachId() {
        return coachId.get();
    }

    public StringProperty coachIdProperty() {
        return coachId;
    }

    public void setCoachId(String coachId) {
        this.coachId.set(coachId);
    }
    public String getPicUrl() {
        return picUrl.get();
    }

    public StringProperty picUrlProperty() {
        return picUrl;
    }

    public void setPicUrl(String picUrl) {
        this.picUrl.set(picUrl);
    }

    public String getName() {
        return name.get();
    }

    public StringProperty nameProperty() {
        return name;
    }

    public void setName(String name) {
        this.name.set(name);
    }

    public String getSex() {
        return sex.get();
    }

    public StringProperty sexProperty() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex.set(sex);
    }

    public String getTele() {
        return tele.get();
    }

    public StringProperty teleProperty() {
        return tele;
    }

    public void setTele(String tele) {
        this.tele.set(tele);
    }

    public String getDetail() {
        return detail.get();
    }

    public StringProperty detailProperty() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail.set(detail);
    }

    public CoachDAO getDao() {
        return dao;
    }

    public void setDao(CoachDAO dao) {
        this.dao = dao;
    }

    public String getPassword() {
        return password.get();
    }

    public StringProperty passwordProperty() {
        return password;
    }

    public void setPassword(String password) {
        this.password.set(password);
    }

    @Override
    public void updateToModel() {
        Coach coach = new Coach(coachId.get(),name.get(),sex.get(),picUrl.get(), tele.get(),detail.get(),password.get());
        dao.update(coach, coachId.get());
    }

    @Override
    public void updateFromModel() {
        Coach coach = dao.selectByID(CoachApp.coachId);
        this.coachId.set(coach.getCoachID());
        this.picUrl.set(coach.getPicUrl());
        this.name.set(coach.getName());
        this.sex.set(coach.getSex());
        this.tele.set(coach.getTele());
        this.detail.set(coach.getDetail());
        this.password.set(coach.getPassword());
    }
}
